package cn.cvs.eduservice.controller;


import cn.cvs.baseservice.utils.R;
import cn.cvs.eduservice.entity.vo.CourseInfoVo;
import cn.cvs.eduservice.entity.vo.CourseQuery;
import cn.cvs.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程
 */
@RestController
@Api(description = "课程管理")
@RequestMapping("/edu/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;


    @ApiOperation(value = "新增课程")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(
            @ApiParam(name = "CourseInfoVo", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo) {
        String eduCourseId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("eduCourseId", eduCourseId);
    }


    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("/getCourseInfo/{id}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        return R.ok().data("items", eduCourseService.getCourseInfoVoById(id));
    }

    @ApiOperation(value = "根据ID修改课程")
    @PostMapping("/updateCourseInfo")
    public R updateCourse(
            @ApiParam(name = "courseInfoVo", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @PostMapping("/{current}/{limit}")
    public R pageQuery(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery) {

        Page<CourseInfoVo> pageParam = new Page<>(current, limit);
        pageParam = eduCourseService.pageQuery(pageParam, courseQuery);
        List<CourseInfoVo> records = pageParam.getRecords();
        long total = pageParam.getTotal();//总数据条数
        return R.ok().data("total", total).data("items", records);
    }


    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        boolean result = eduCourseService.removeCourseById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

